package com.eh.taxation.security.config;

import com.eh.taxation.security.filter.TokenAuthenticationFilter;
import com.eh.taxation.security.filter.TokenLoginFilter;
import com.eh.taxation.security.security.DefaultPasswordEncoder;
import com.eh.taxation.security.security.TokenLogoutHandler;
import com.eh.taxation.security.security.TokenManager;
import com.eh.taxation.security.security.UnauthorizedEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * <p>
 * Security核心配置类
 * </p>
 *
 * @author qy
 * @since 2019-11-18
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {

    private UserDetailsService userDetailsService;  // 自定义查询数据库
    private TokenManager tokenManager;  // token操作工具
    private DefaultPasswordEncoder defaultPasswordEncoder; // 密码加密工具
    private RedisTemplate redisTemplate; // redis 操作

    @Autowired
    public TokenWebSecurityConfig(UserDetailsService userDetailsService, DefaultPasswordEncoder defaultPasswordEncoder,
                                  TokenManager tokenManager, RedisTemplate redisTemplate) {
        this.userDetailsService = userDetailsService;
        this.defaultPasswordEncoder = defaultPasswordEncoder;
        this.tokenManager = tokenManager;
        this.redisTemplate = redisTemplate;
    }

    //  配置设置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()
                .authenticationEntryPoint(new UnauthorizedEntryPoint()) // 设置没有权限访问时，所调用的处理类
                .and().csrf().disable()// 关闭csrf
                .authorizeRequests()
                .anyRequest().authenticated()
                .and().logout().logoutUrl("/admin/acl/index/logout") // 设置退出路径
                .addLogoutHandler(new TokenLogoutHandler(tokenManager,redisTemplate)).and() // 设置退出所调用的处理类
                .addFilter(new TokenLoginFilter(authenticationManager(), tokenManager, redisTemplate)) // 设置自定义过滤器-------- 认证过滤器
                .addFilter(new TokenAuthenticationFilter(authenticationManager(), tokenManager, redisTemplate)).httpBasic(); // 设置自定义过滤器-------- 授权过滤器
    }

    // 设置userDetailsService以及自定义密码处理类
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(defaultPasswordEncoder);
    }

    // 配置哪些请求不拦截
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/api/**",
                "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**"
               );
    }
}